package com.hmbe.controller;

import com.hmbe.dto.Result;
import com.hmbe.dto.StatusCode;
import com.hmbe.entity.HmBeCategory;
import com.hmbe.service.HmBeCategoryService;
import com.hmbe.utils.GetUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;

@RestController
@RequestMapping("/my/cate")
public class HmBeArticleCateGoryController {
    @Autowired
    private HmBeCategoryService hmBeCategoryService;

    @DeleteMapping("/del")
    public Result delete(@RequestParam("id") String id) {
        hmBeCategoryService.removeById(Integer.valueOf(id));
        return Result.ok("删除文章分类成功！", StatusCode.SUCCESS);
    }

    @PutMapping("/info")
    public Result update(@RequestBody HmBeCategory hmBeCategory, HttpServletRequest httpServletRequest) {
        Long userId = GetUserUtils.getUserId((httpServletRequest));
        hmBeCategoryService.updateById(hmBeCategory);
        return Result.ok("新增文章分类成功！", StatusCode.SUCCESS);
    }

    @GetMapping("/list")
    public Result list(HttpServletRequest httpServletRequest) {
        return hmBeCategoryService.getCategoryList(httpServletRequest);
    }

    @PostMapping("/add")
    public Result addCategory(@RequestBody HmBeCategory hmBeCategory, HttpServletRequest httpServletRequest) {
        Long userId = GetUserUtils.getUserId((httpServletRequest));
        hmBeCategory.setUserId(Long.valueOf(userId));
        //LocalDateTime获取当前时间
        hmBeCategory.setCreateTime(new Date());
        hmBeCategoryService.save(hmBeCategory);
        return Result.ok("更新文章分类成功！", StatusCode.SUCCESS);
    }


}
